<template>
  <div class="row">
    <div class="option">
      <!-- name 单选框 一组 name属性的作用是将你的单选框按钮分为一组，实现点击一个，另外的取消点击功能。如若不然，每个点击按钮可以同时点击。
      id 和 label 中的for 是 一组 id和for的值保持一致 点击label中的文字也可以选中 -->
      <input
        :type="type === '1' ? 'radio' : 'checkbox'"
        :checked="item.isRight"
        name="option"
        id="option"
        @change="getChecked"
      />
      <label for="option">{{ item.code }}:</label>
    </div>
    <el-input v-model="item.title"></el-input>
    <div class="image" v-if="item.img">
      <!-- item.img -->
      <img :src="item.img" alt="" />
      <i
        class="el-icon-circle-close icon"
        @click="$emit('getImg', '', item.code)"
      ></i>
    </div>
    <div class="upload" v-else @click="$refs.uploadFile.click()">上传图片</div>
    <input
      type="file"
      style="display: none"
      ref="uploadFile"
      @change="upload"
    />
  </div>
</template>

<script>
export default {
  name: 'options-pages',
  props: {
    item: {
      type: Object,
      required: true
    },
    type: {
      type: String, // 1是单选  2是多选
      required: true
    }
  },
  data () {
    return {
      img: ''
    }
  },
  methods: {
    upload (e) {
      this.$emit(
        'getImg',
        URL.createObjectURL(e.target.files[0]),
        this.item.code
      )
      // 解决上传两张重复图片的问题
      e.target.value = ''
    },
    getChecked (e) {
      console.log(e.target.checked)
      this.$emit('getChecked', e.target.checked, this.item.code)
    }
  }
}
</script>

<style scoped lang="scss">
.row {
  display: flex;
  // justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  .option {
    width: 40px;
    margin-right: 10px;
    label {
      margin-left: 10px;
      font-weight: normal;
    }
  }
  .el-input {
    width: 60%;
    margin-right: 15px;
  }
  .en {
    margin-left: 5px;
  }
  .upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    width: 100px;
    height: 60px;
    line-height: 60px;
    text-align: center;
  }
  .image {
    position: relative;
    width: 100px;
    height: 60px;
    img {
      width: 100%;
      height: 100%;
    }
    .icon {
      position: absolute;
      right: 6px;
      top: -5px;
      transform: translate(50%, -50%);
      background: #fff;
      font-size: 18px;
      color: #999;
    }
  }
}
</style>
